package com.guoxue.action.front;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoxue.domain.AlbumVo;
import com.guoxue.domain.UserVo;
import com.guoxue.service.IAlbumService;
import com.guoxue.service.IUserService;
import com.guoxue.util.OSSUtil;
import com.guoxue.util.OtherUtils;
@Controller
@RequestMapping("User")
/**
 * 用户相册处理接口，主要包括：
 * 1.uploadPhotos增加相册及相片
 * 2.delPhotos删除或者修改相册及相册的照片
 * 3.getPhotosList查询用户的相册
 * @author kanghb
 */
public class PhotoAction extends BaseAction{
	@Resource
	private IUserService userService;	
	@Resource
	private IAlbumService albumService;	
	@Resource
	private OSSUtil oss;
	
	
	/**
	 * 用户上传照片接口
	 * @param req
	 * @param resp
	 * @param uid
	 * @param ossKey
	 * @return
	 */
	@RequestMapping("/uploadPhotos")
	@ResponseBody
	public JSON uploadPhotos(HttpServletRequest req, HttpServletResponse resp, Integer uid, String ossKey){
		if (uid==null){
			return Message(ERROR, "用户id为空", null);
		}
		if (ossKey==null){
			return Message(ERROR, "未提交osskey", null);
		}
		AlbumVo al = new AlbumVo();
		al.setOssKey(ossKey);
		al.setUid(uid);
		al.setUploadDate(OtherUtils.getCurrentDate());
		albumService.saveAlbum(al);
		return Message(SUCCESS, "保存成功", null);
	}
	/**
	 * 删除照片或修改头像接口 
	 * @param req
	 * @param resp
	 * @param photoId
	 * 			照片id
	 * @param type
	 * 			操作类型 1：删除。2：修改头像
	 * @return json
	 */
	@RequestMapping("/changePhotoStatus")
	@ResponseBody
	public JSON delPhotos(HttpServletRequest req, HttpServletResponse resp,Integer photoId, Integer type){
		if (photoId==null){
			return Message(ERROR, "未提交照片id", null);
		}
		if (type==null){
			return Message(ERROR, "未指定操作类型", null);
		}
		AlbumVo album = null;			
		album = albumService.getAlbumByKey(photoId);
		if (album==null){
			return Message(SUCCESS, "删除照片失败，为查找到对应照片", null);
		}
		if (type==1){
				album.setSysflag(1);
				album.setUploadDate(OtherUtils.getCurrentDate());
				albumService.saveAlbum(album);
				UserVo user = userService.getUserByKey(album.getUid());
				if (user.getSex()==1&&user.getImgUrl().equals(album.getOssKey())){
					user.setImgUrl("defaultHeadM.png");
					userService.saveUser(user);
				}
				if (user.getSex()==2&&user.getImgUrl().equals(album.getOssKey())){
					user.setImgUrl("defaultHeadG.png");
					userService.saveUser(user);
				}
				return Message(SUCCESS, "删除照片成功", isEmpty(album.getOssKey())?null:oss.getUrl(bucket, user.getImgUrl(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
		}
		if (type==2){
			UserVo user = userService.getUserByKey(album.getUid());
			user.setImgUrl(album.getOssKey());
			userService.saveUser(user);
			return Message(SUCCESS, "头像修改成功", isEmpty(album.getOssKey())?null:oss.getUrl(bucket, album.getOssKey(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
		}
		return null;
	}
	/**
	 * 获取用户相册
	 * @param req
	 * @param resp
	 * @param uid
	 * 			用户id
	 * @return
	 */
	@RequestMapping("getPhotosList")
	@ResponseBody
	public JSON getPhotosList(HttpServletRequest req, HttpServletResponse resp, Integer id){
		if (id==null){
			return Message(ERROR, "未提交用户id", null);
		}
		List<AlbumVo> list = albumService.getPhotosList(id);
		JSONArray array = new JSONArray();
		if (list.size()>0){
			UserVo user = userService.getUserByKey(id);
			for(AlbumVo a:list){
				JSONObject obj = new JSONObject();
				obj.put("albumId", a.getId());
				obj.put("url", oss.getUrl(bucket, a.getOssKey(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
				if (a.getOssKey().equals(user.getImgUrl())){
					obj.put("currentImg", true);
				}else{
					obj.put("currentImg", false);
				}
				array.add(obj);
			}
		}
		return Message(SUCCESS, "获取照片列表成功", array.size()>0?array:null);
	}
}
